package com.edusite
import java.text.SimpleDateFormat

class AvisosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
    if (session.user.tipo != "admin")
    	redirect(uri:"/")
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [avisosInstanceList: Avisos.list(params), avisosInstanceTotal: Avisos.count()]
    }
        def listPublic = {
           params.max = Math.min(params.max ? params.int('max') : 10, 100)
//           [avisosInstanceList: Avisos.list(params), avisosInstanceTotal: Avisos.count()]
           [avisosInstanceList: Avisos.findAllByAmbito("publico",params), avisosInstanceTotal: Avisos.count()]

    }
            def listAlumno = {
           params.max = Math.min(params.max ? params.int('max') : 10, 100)
//           [avisosInstanceList: Avisos.list(params), avisosInstanceTotal: Avisos.count()]
	    def listado =Avisos.findAllByAmbitoOrAmbito("publico","privado",params)
	    listado = listado + Avisos.findAllByEnviadopor(session.user,params)
           [avisosInstanceList: listado, avisosInstanceTotal: Avisos.count()]

    }
    
            def listProfe = {
           params.max = Math.min(params.max ? params.int('max') : 10, 100)
//           [avisosInstanceList: Avisos.list(params), avisosInstanceTotal: Avisos.count()]
	    def listado =Avisos.findAllByAmbitoOrAmbito("publico","privado",params)
	    listado = listado + Avisos.findAllByEnviadopor(session.user,params)
           [avisosInstanceList: listado, avisosInstanceTotal: Avisos.count()]

    }


    def create = {
    if (session.user.tipo != "admin")
    	redirect(uri:"/")
        def avisosInstance = new Avisos()
        avisosInstance.properties = params
 //       avisosInstance.fechaenv = new Date()
        return [avisosInstance: avisosInstance]
    }

    def createAlumno = {
    if (session.user.tipo !='alumno')
    	redirect(uri:"/home/indice")
        def avisosInstance = new Avisos()
        avisosInstance.properties = params
 //       avisosInstance.fechaenv = new Date()
        return [avisosInstance: avisosInstance]
    }
    def createProfe = {
    if (session.user.tipo !='profesor')
    	redirect(uri:"/home/indice")
        def avisosInstance = new Avisos()
        avisosInstance.properties = params
 //       avisosInstance.fechaenv = new Date()
        return [avisosInstance: avisosInstance]
    }

    def save = {
        def avisosInstance = new Avisos(params)
        avisosInstance.fechaenv = new Date()
        if (avisosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'avisos.label', default: 'Avisos'), avisosInstance.id])}"
            if (session.user.tipo=='admin')
               redirect(action: "show", id: avisosInstance.id)
            else if (session.user.tipo=='alumno')
               redirect(action: "showAlumno", id: avisosInstance.id)
            else if (session.user.tipo=='profesor')
               redirect(action: "showProfe", id: avisosInstance.id)
            else 
                redirect(uri:"/")
            
            
        }
        else {
            render(view: "create", model: [avisosInstance: avisosInstance])
        }
    }

    def show = {
        def avisosInstance = Avisos.get(params.id)
        if (!avisosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [avisosInstance: avisosInstance]
        }
    }

    def showPublic = {
        def avisosInstance = Avisos.get(params.id)
       
        if (!avisosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
        else {
            if (avisosInstance.publico) {
                [avisosInstance: avisosInstance]
                }
            else {
                flash.message= "No está autorizado a ver avisos que no sean públicos. Autentifiquese o registrese"
                
            }
        }
    }


    def showAlumno = {
        def avisosInstance = Avisos.get(params.id)
        if (!avisosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [avisosInstance: avisosInstance]
        }
    }
    
    def showProfe = {
        def avisosInstance = Avisos.get(params.id)
        if (!avisosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [avisosInstance: avisosInstance]
        }
    }


    def edit = {
        def avisosInstance = Avisos.get(params.id)
        if (!avisosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [avisosInstance: avisosInstance]
        }
    }
    def dia = {
    
	   Date eldia= new Date()
	    def listado =Avisos.findAllByAmbitoOrAmbito("publico","privado",params)
	    listado = listado + Avisos.findAllByEnviadopor(session.user,params)
	    
           [listado:listado, eldia:eldia]

    }

    def update = {
        def avisosInstance = Avisos.get(params.id)
        if (avisosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (avisosInstance.version > version) {
                    
                    avisosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'avisos.label', default: 'Avisos')] as Object[], "Another user has updated this Avisos while you were editing")
                    render(view: "edit", model: [avisosInstance: avisosInstance])
                    return
                }
            }
            avisosInstance.properties = params
            if (!avisosInstance.hasErrors() && avisosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'avisos.label', default: 'Avisos'), avisosInstance.id])}"
                redirect(action: "show", id: avisosInstance.id)
            }
            else {
                render(view: "edit", model: [avisosInstance: avisosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def avisosInstance = Avisos.get(params.id)
        if (avisosInstance) {
            try {
                avisosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'avisos.label', default: 'Avisos'), params.id])}"
            redirect(action: "list")
        }
    }
    
 def events = { 
    render(contentType: "text/xml") { 
       data() {
        String startDate = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US).format(new Date()) + " GMT"  
        event('start': startDate, 'title':"Nice title", 'link':"http://localhost:8080/educativo/avisos/show/1", "This is a description")

startDate = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US).format(new Date() + 20) + " GMT" 

event('start': startDate, 'title':"Hello World", 'link':"http://localhost:8080/educativo/avisos/show/2", "This is another description")



startDate = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US).format(new Date() + 40) + " GMT" 

String endDate = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US).format(new Date() + 45) + " GMT" 

event('start': startDate, 'end': endDate, 'title':"Hello again", 'link':"http://localhost:8080/educativo/avisos/show/3", "This is another description")

event('start': startDate, 'title':"Hello again", 'link':"http://localhost:8080/educativo/avisos/show/3", textColor: "#FF0000", color: "#FFFF00", icon:"http://simile.mit.edu/timeline/api/images/dark-red-circle.png", "This is another description")
 


	}
     } 
  }    
    
    
}
